<?php
namespace Admin\Controller;
use Common\Controller\BackendController;

class DataokeController extends BackendController
{
    protected $appkey = '5a6189c740';
    protected $api = 'http://api.dataoke.com/index.php';
    public function _initialize() {
        parent::_initialize();
        $this->assign([
            'v' => $this->v,
            'appkey' => $this->appkey,
        ]);
    }

    /**
     * 网站专用API接口
     */
    public function goodsLink()
    {
        $type = 'www_quan';
        if(IS_POST) {
            $page = I('post.page');
            $p = 1;
            while($p <= $page) {
                $url = $this->api.'?r=goodsLink/www&type='.$type.'&appkey='.$this->appkey.'&v=2&page='.$page;
                $data = file_get_contents($url);
                $msg = $this->add($data);
                $p++;
            }
            $msg['p'] = $page;
            $this->assign('msg',$msg);
        }

        $this->assign([
           'type'=>$type,
        ]);
        $this->display();
    }

    /**
     * QQ群发专用API接口
     */
    public function qqLink()
    {
        $type = 'qq_quan';
        if(IS_POST) {
            $page = I('post.page');
            $p = 1;
            while($p <= $page) {
                $url = $this->api.'?r=port/index&type='.$type.'&appkey='.$this->appkey.'&v=2&page='.$page;
                $data = file_get_contents($url);
                $msg = $this->add($data);
                $p++;
            }
            $msg['p'] = $page;
            $this->assign('msg',$msg);
        }

        $this->assign([
            'type'=>$type,
        ]);
        $this->display();
    }

    /**
     * 全站领券商品API接口
     */
    public function portLink()
    {
        $type = 'total';
        if(IS_POST) {
            $page = I('post.page');
            $p = 1;
            while($p <= $page) {
                $url = $this->api.'?r=port/index&type='.$type.'&appkey='.$this->appkey.'&v=2&page='.$page;
                $data = file_get_contents($url);
                echo $url;
                $msg = $this->add($data);
                $p++;
            }
            $msg['p'] = $page;
            $this->assign('msg',$msg);
        }

        $this->assign([
            'type'=>$type,
        ]);
        $this->display();
    }

    /**
     * TOP100人气榜API接口
     */
    public function topLink()
    {
        $type = 'top100';
        if(IS_POST) {
            $url = $this->api.'?r=port/index&type='.$type.'&appkey='.$this->appkey.'&v=2';
            $data = file_get_contents($url);
            $msg = $this->add($data);
            $this->assign('msg',$msg);
        }

        $this->assign([
            'type'=>$type,
        ]);
        $this->display();
    }

    /**
     * 实时跑量榜API接口
     */
    public function numLink()
    {
        $type = 'paoliang';

        if(IS_POST) {
            $url = $this->api.'?r=port/index&type='.$type.'&appkey='.$this->appkey.'&v=2';
            $data = file_get_contents($url);
            $msg = $this->add($data);
            $this->assign('msg',$msg);
        }


        $this->assign([
            'type'=>$type,
        ]);
        $this->display();
    }

    /**
     * 单品详情API接口
     */
    public function singleLink()
    {
        $this->display();
    }

    public function add($items)
    {
        $model = D('Items');
        $msg = $model->dataoke($items);
        return $msg;
    }
}
